﻿using JetBrains.Annotations;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;

namespace GammaJul.ReSharper.GammaXaml.Psi.CSharp {

	/// <summary>
	/// Contains extension methods related to PSI, specific to C#.
	/// </summary>
	public static class CSharpPsiExtensions {

		/// <summary>
		/// Gets an argument by its parameter name.
		/// </summary>
		/// <param name="argumentsOwner">The arguments owner.</param>
		/// <param name="name">The name of the argument to return.</param>
		/// <returns>A <see cref="ICSharpArgument"/> corresponding to <paramref name="name"/>, or <c>null</c> if none was found.</returns>
		[CanBeNull]
		public static ICSharpArgument GetArgumentByParameterName([NotNull] this ICSharpArgumentsOwner argumentsOwner, [CanBeNull] string name) {
			foreach (ICSharpArgument argument in argumentsOwner.Arguments) {
				DeclaredElementInstance<IParameter> matchingParameter = argument.MatchingParameter;
				if (matchingParameter != null && matchingParameter.Element.ShortName == name)
					return argument;
			}
			return null;
		}

	}

}